package com.efuture.servlet.response;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * @description: 演示重定向跳转
 * @author zhaoshanshan
 * @date 2021/10/3 11:09 上午
 * @version 1.0
 */
@WebServlet("/RedirectTestOne")
public class RedirectTestOne extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("RedirectTestOne被访问了");
        //设置重定向
//        response.setStatus(302);
//        response.setHeader("location","/RedirectTestTwo");

        //重定向的简单写法
        String path = request.getContextPath();//动态获取虚拟目录
        System.out.println(path);
        if(null!=path&&path.length()>0){
            response.sendRedirect(path+"/RedirectTestTwo");
        }else{
            response.sendRedirect("/RedirectTestTwo");
        }
    }
}
